package com.hhh.demo.sx.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hhh.demo.sx.bean.UserRole;
import com.hhh.demo.sx.service.UserRoleService;
import com.hhh.demo.sx.util.ResultMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hhh
 * @since 2021-12-27
 */
@RestController
@RequestMapping("/user_role")
public class UserRoleController {

    @Autowired
    private UserRoleService userRoleService;

    @RequestMapping("/change")
    public ResultMap change(UserRole userRole,boolean f){
        QueryWrapper<UserRole> wrapper =new QueryWrapper<>();
        wrapper.eq("user_id",userRole.getUserId())
                .eq("role_id",userRole.getRoleId());
        if(f&&userRoleService.getOne(wrapper)==null){
            userRoleService.saveOrUpdate(userRole);
        }else {

            userRoleService.remove(wrapper);
        }


        return ResultMap.success();
    }
    @RequestMapping("/getOne")
    public ResultMap change(int id){

        QueryWrapper<UserRole> wrapper =new QueryWrapper<>();
        wrapper.eq("user_id",id);
        return ResultMap.success(userRoleService.list(wrapper));
    }
}

